package com.mrjy.maintain.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.dto.SMSSendDto;
import com.mrjy.common.dto.UserParaDto;
import com.mrjy.common.entity.SMSSendInfo;
import com.mrjy.common.entity.UserPwd;
import com.mrjy.maintain.bo.ISMSSendInfoBo;
import com.mrjy.maintain.bo.IUserPwdBo;


@RequestMapping("/maintain/userPwd")
@Controller
public class UserPwdController {

	@Resource
	private IUserPwdBo userPwdBo;
	
	@Resource
	private ISMSSendInfoBo smsSendInfoBo;
	
	/**
	 * 邮箱方式验证
	 * @param userParaDto
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/pwdValidateEmail")
	@ResponseBody
	public Object pwdValidateEmail(UserParaDto userParaDto ,  HttpServletRequest request){
		userParaDto.setIp(request.getRemoteAddr());
		String mark = userPwdBo.txSafeValidateEmail(userParaDto);
		return mark;
	}
	
	/**
	 * 验证手机并发送验证码
	 * @param phone
	 * @param session
	 * @return
	 */
	@RequestMapping("/phoneValidate")
	@ResponseBody
	public Object phoneValidate(String mobilePhone,HttpSession session){
		String flag=(String) userPwdBo.phoneValidate(mobilePhone);
		if(flag=="ok"){
			SMSSendDto smsSendDto=new SMSSendDto();
			smsSendDto.setRecNum(mobilePhone);
			smsSendDto.setProduct("365明人课堂");
			smsSendDto.setTypeId(8);
			int result= smsSendInfoBo.sendSMS(smsSendDto);
			if(result==0){
				return "sendError";
			}else{
				return flag;
			}
		}else {
			return flag;
		}
	
	}
	
	/**
	 * 手机方式验证验证码
	 * @param userParaDto
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/pwdValidatePhone")
	@ResponseBody
	public Object pwdValidatePhone(UserParaDto userParaDto ,HttpSession session,  HttpServletRequest request){
		//HashMap<String, String> map = (HashMap<String, String>) session.getAttribute(GlobalConstant.RANDOM_PhoneCODE_KEY);
		
		String flag=(String) userPwdBo.phoneValidate(userParaDto.getMobilePhone());
		if(flag=="ok"){
			SMSSendInfo smsSendInfo=new SMSSendInfo();
			smsSendInfo.setRecNum(userParaDto.getMobilePhone());
			smsSendInfo.setSendCode(userParaDto.getMobileValCode());		
			smsSendInfo.setSmsCode("SMS_16440022");
			smsSendInfo=smsSendInfoBo.validateSMSCode(smsSendInfo);
			if(smsSendInfo.getSmsMessage().equals("ok")){
				userParaDto.setIp(request.getRemoteAddr());
				String mark = userPwdBo.safeValidatePhone(userParaDto);
				return mark;
			}else {
				return smsSendInfo.getSmsMessage();
			}
		}else{
			return "misMatch";
		}
		
		
	}
	

	/**
	 * 密码重置有效性检测
	 * @param userPwd
	 * @return
	 */
	@RequestMapping("/expireValidate")
	@ResponseBody
	public Object expireValidate(UserPwd userPwd){
		return userPwdBo.expireValidate(userPwd);
	}
	
	/**
	 * 密码重置
	 * @param userInfo
	 * @return
	 */
	@RequestMapping("/changePassword")
	@ResponseBody
	public Object changePassword(UserPwd userPwd){
		return userPwdBo.txChangePassword(userPwd);
	}
	
	/**
	 * 密码重置
	 * @param mobilePhone
	 * @param validCode
	 * @param newPwd
	 * @param confirmPwd
	 * @author csshui
	 * @return
	 */
	@RequestMapping("/resetPwd")
	@ResponseBody
	public Object resetPwd(@RequestParam("mobilePhone")String mobilePhone ,@RequestParam("validCode")String validCode
			,@RequestParam("newPwd")String newPwd,@RequestParam("confirmPwd")String confirmPwd,@RequestParam("typeId")Integer typeId){
		return userPwdBo.txRetPwd(mobilePhone,validCode, newPwd, confirmPwd,typeId);
	}
	
	
}
